package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.CheckGroupItemMapper;
import com.itheima.pojo.CheckGroupItem;
import com.itheima.service.CheckGroupItemService;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.ArrayList;
import java.util.Arrays;

@DubboService
public class CheckGroupItemServiceImpl extends ServiceImpl<CheckGroupItemMapper, CheckGroupItem> implements CheckGroupItemService {

    /**
     * 删除所选的数据关联的中间表
     * @param id CheckGroupVo 传过来的id -> CheckGroup的ID
     */
    @Override
    public void deleteByGroupId(Long id) {
        //delete from tb_group_item
        QueryWrapper<CheckGroupItem> queryWrapper = new QueryWrapper<>();

        // where group_id=#{id}
        queryWrapper.eq("group_id",id);

        remove(queryWrapper);

    }

    /**
     * 保存删除后所选择的中间表
     * @param id CheckGroupVo 传过来的id -> CheckGroup的ID
     * @param items CheckGroupVo 传过来的items 数组 ->CheckItem 的 id
     */
    @Override
    public void saveCheckGroupItem(Long id, Long[] items) {
        if (items!=null&&items.length>0){
            ArrayList<CheckGroupItem> list = new ArrayList<>();
            // TODO : stram 流
            Arrays.stream(items).forEach((itemId)->{
                CheckGroupItem checkGroupItem = new CheckGroupItem();
                checkGroupItem.setItem_id(itemId);
                checkGroupItem.setGroup_id(id);
                list.add(checkGroupItem);
            });
            this.saveOrUpdateBatch(list);
        }

    }

    @Override
    public Long[] selectItemsByGroupId(Long groupId) {
        return this.baseMapper.selectItemsByGroupId(groupId);
    }

}
